package cn.imvc.app.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.imvc.app.entities.Commission;
import cn.imvc.app.entities.Member;
import cn.imvc.app.models.CommissionModel;
import cn.imvc.app.models.MemberModel;

@Controller
@RequestMapping("commission")
public class CommissionControllerImpl implements CommissionController {

	@Autowired
	MemberModel memberModel;

	@Autowired
	CommissionModel commissionModel;

	@RequestMapping("performance.html")
	public String performance(Model model, String token) {
		// 用户未登录状态直接跳转登陆页面
		if (token == null || !memberModel.checkLogin(token)) {
			return "redirect:/member/login.html";
		}
		// 通过token从缓存里取member
		Member member = memberModel.getLoginMember(token);
		member = memberModel.get(member.getId());
		model.addAttribute("points", member.getAccount().getPoints());
		model.addAttribute("isMember", member.getDealer().getDealerLevel().getType() == (byte)0);
		model.addAttribute("commpoints", commissionModel.findCommPointsByMember(member).get(0));
		return "commission/performance";
	}
	
	@RequestMapping("commission.html")
	public String commission(Model model, String token, String type) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			model.addAttribute("type", type);
			return "commission/commission";
		} else {
			return "redirect:/member/login.html";
		}
	}
	
	@RequestMapping("commissionlist.html")
	public String commissionlist(Model model, String token, Byte type, Integer page) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			PageRequest pageRequest = new PageRequest(page, 9, null);
			Page<Commission> result = commissionModel.findOrderPointsByMember(member, type, pageRequest);
			model.addAttribute("commissionList", result.getContent());
			model.addAttribute("over", page == result.getTotalPages() ? true : false);
			return "commission/commissionlist";
		} else {
			return "redirect:/member/login.html";
		}
	}

	@RequestMapping("performance.htm")
	public String pcperformance(Model model, String token) {
		// 用户未登录状态直接跳转登陆页面
		if (token == null || !memberModel.checkLogin(token)) {
			return "redirect:/member/login.htm";
		}
		// 通过token从缓存里取member
		Member member = memberModel.getLoginMember(token);
		member = memberModel.get(member.getId());
		model.addAttribute("points", member.getAccount().getPoints());
		model.addAttribute("isMember", member.getDealer().getDealerLevel().getType() == (byte)0);
		model.addAttribute("commpoints", commissionModel.findCommPointsByMember(member).get(0));
		return "commission/pcperformance";
	}
	
	@RequestMapping("commissionlist.htm")
	public String pccommissionlist(Model model, String token, Byte type, Integer page) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			page--;
			PageRequest pageRequest = new PageRequest(page, 9, null);
			Page<Commission> result = commissionModel.findOrderPointsByMember(member, type, pageRequest);
			model.addAttribute("commissionList", result.getContent());
			model.addAttribute("over", page == result.getTotalPages() ? true : false);
			return "commission/pccommissionlist";
		} else {
			return "redirect:/member/login.html";
		}
	}

}
